package cn.edu.Controller;

import cn.edu.DAO.OrderDAOByXML;
import cn.edu.DAO.UserDAOByXML;
import cn.edu.DO.OrderDO;
import cn.edu.DO.UserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ManagerUser {
@Autowired
    UserDAOByXML userDAO;
@Autowired
    OrderDAOByXML orderDAO;
    @RequestMapping("/adminUserAdd")
    public String adminUserAdd(){
        return "admin/UserAdd";
    }
@RequestMapping("/AddUser")
public String insertUser(HttpServletRequest request){
    OrderDO ord = new OrderDO();
    ord.setId(request.getParameter("id"));
    ord.setSid(request.getParameter("sid"));
    ord.setOid(request.getParameter("oid"));
    ord.setDate(request.getParameter("date"));
    orderDAO.insertOrder(ord);
//    System.out.println(orderDAO.insertOrder(ord));
    return "admin/AddSuccess";
}
    @RequestMapping("/adminDeleteUser")
    public String adminDeleteUser(){
        return "admin/DeleteUser";
    }
@RequestMapping("/DeleteUser")
    public ModelAndView deleteUser(HttpServletRequest request, Model model){
    UserDO user = new UserDO();
    user.setUsername(request.getParameter("username"));
    ModelAndView mv = new ModelAndView();
    if(userDAO.deleteUser(user)!=null){
        System.out.println(userDAO.deleteUser(user));
        mv.setViewName("admin/DeleteSuccess");
    }
    else {
        model.addAttribute("error","所要删除用户不存在");
        mv.setViewName("admin/DeleteUser");
    }
    return mv;
}
@RequestMapping("/UpdateOrder")
        public String UpdateOrder(){
        return "admin/UpdateOrder";
}

@RequestMapping("/OrderUpdate")
    public String UpdateOrder(HttpServletRequest request){
    OrderDO order = new OrderDO();
    order.setId(request.getParameter("id"));
    order.setSid(request.getParameter("sid"));
    order.setOid(request.getParameter("oid"));
    order.setDate(request.getParameter("date"));
    orderDAO.updateOrder(order);
    return "admin/UpdateSuccess";
}
}
